
 <!DOCTYPE HTML>
<html >
<head>
  <meta charset="UTF-8">
  
    <title>基于业务的列表比较器 | Typhoon&#39;s code space</title>
    <meta name="viewport" content="width=device-width, initial-scale=1,user-scalable=no">
    
    <meta name="author" content="typhoon">
    

    
    <meta name="description" content="在很多情况下前端页面或者其他客户端和后台交互提交数据都是单条数据的更新和插入,但是在有些场景下, 基于特定的业务客户端需要一列表的方式提交数据,我们传统的解决方案是讲苦中的数据删除,然后将客户端 传来的数据列表批量插入,但是这样就有很多弊.">
<meta name="keywords" content="基于业务的列表比较器">
<meta property="og:type" content="article">
<meta property="og:title" content="基于业务的列表比较器">
<meta property="og:url" content="http://scorpioaeolus.oschina.io/2017/09/17/基于业务的列表比较器/index.html">
<meta property="og:site_name" content="Typhoon&#39;s code space">
<meta property="og:description" content="在很多情况下前端页面或者其他客户端和后台交互提交数据都是单条数据的更新和插入,但是在有些场景下, 基于特定的业务客户端需要一列表的方式提交数据,我们传统的解决方案是讲苦中的数据删除,然后将客户端 传来的数据列表批量插入,但是这样就有很多弊.">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2017/09/17/基于业务的列表比较器/1.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2017/09/17/基于业务的列表比较器/2.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2017/09/17/基于业务的列表比较器/3.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2017/09/17/基于业务的列表比较器/4.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2017/09/17/基于业务的列表比较器/5.png">
<meta property="og:updated_time" content="2017-09-17T13:48:53.187Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="基于业务的列表比较器">
<meta name="twitter:description" content="在很多情况下前端页面或者其他客户端和后台交互提交数据都是单条数据的更新和插入,但是在有些场景下, 基于特定的业务客户端需要一列表的方式提交数据,我们传统的解决方案是讲苦中的数据删除,然后将客户端 传来的数据列表批量插入,但是这样就有很多弊.">
<meta name="twitter:image" content="http://scorpioaeolus.oschina.io/typhoon/2017/09/17/基于业务的列表比较器/1.png">

    
    <link rel="alternative" href="/atom.xml" title="Typhoon&#39;s code space" type="application/atom+xml">
    
    
    <link rel="icon" href="/typhoon/img/java.png">
    
    
    <link rel="apple-touch-icon" href="/typhoon/img/jacman.jpg">
    <link rel="apple-touch-icon-precomposed" href="/typhoon/img/jacman.jpg">
    
    <link rel="stylesheet" href="/typhoon/css/style.css">
</head>

  <body>
    <header>
      
<div>
		
			<div id="imglogo">
				<a href="/typhoon/"><img src="/typhoon/img/java.png" alt="Typhoon&#39;s code space" title="Typhoon&#39;s code space"/></a>
			</div>
			
			<div id="textlogo">
				<h1 class="site-name"><a href="/typhoon/" title="Typhoon&#39;s code space">Typhoon&#39;s code space</a></h1>
				<h2 class="blog-motto"></h2>
			</div>
			<div class="navbar"><a class="navbutton navmobile" href="#" title="Menu">
			</a></div>
			<nav class="animated">
				<ul>
					<ul>
					 
						<li><a href="/typhoon/">主页</a></li>
					
						<li><a href="/typhoon/archives">归档</a></li>
					
						<li><a href="/typhoon/about">关于</a></li>
					
					<li>
 					
					<form class="search" action="//google.com/search" method="get" accept-charset="utf-8">
						<label>Search</label>
						<input type="search" id="search" name="q" autocomplete="off" maxlength="20" placeholder="Search" />
						<input type="hidden" name="q" value="site:scorpioaeolus.oschina.io">
					</form>
					
					</li>
				</ul>
			</nav>			
</div>
    </header>
    <div id="container">
      <div id="main" class="post" itemscope itemprop="blogPost">
  
	<article itemprop="articleBody"> 
		<header class="article-info clearfix">
  <h1 itemprop="name">
    
      <a href="/typhoon/2017/09/17/基于业务的列表比较器/" title="基于业务的列表比较器" itemprop="url">基于业务的列表比较器</a>
  </h1>
  <p class="article-author">By
       
		<a href="/typhoon/about" title="typhoon" target="_blank" itemprop="author">typhoon</a>
		
  <p class="article-time">
    <time datetime="2017-09-17T13:27:21.000Z" itemprop="datePublished"> Published 2017-09-17</time>
    
  </p>
</header>
	<div class="article-content">
		
		<div id="toc" class="toc-article">
			<strong class="toc-title">Contents</strong>
		
			<ol class="toc"><li class="toc-item toc-level-5"><a class="toc-link" href="#背景"><span class="toc-number">1.</span> <span class="toc-text">背景</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#问题"><span class="toc-number">2.</span> <span class="toc-text">问题</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#解决方案"><span class="toc-number">3.</span> <span class="toc-text">解决方案</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#实现方式"><span class="toc-number">4.</span> <span class="toc-text">实现方式</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#总结"><span class="toc-number">5.</span> <span class="toc-text">总结</span></a></li></ol>
		
		</div>
		
		<pre><code>在很多情况下前端页面或者其他客户端和后台交互提交数据都是单条数据的更新和插入,但是在有些场景下,
基于特定的业务客户端需要一列表的方式提交数据,我们传统的解决方案是讲苦中的数据删除,然后将客户端
传来的数据列表批量插入,但是这样就有很多弊.
</code></pre><a id="more"></a>
<pre><code>弊端:
1)有些数据根本没有变动,而经历了一此删除和插入,
2)增加了数据库交互次数,删除和插入会带来数据锁定,从而带来额外的性能损耗。接下来我们将根据
实际案例分析来实现将提交数据列表和库中数据对比来避免上述问题
</code></pre><h5 id="背景"><a href="#背景" class="headerlink" title="背景"></a>背景</h5><pre><code>在crm2.0系统退费业务中,门店红娘主任发起退费申请,需要上传相应的pos小票,解除服务协议,委托书等图片信息,
图片上传到资源服务器后会将信息存放到RefundInfoImg表
中通过退费id和退费单关联;此后需要门店财务审批和退费组核实操作,这两步骤操作人员都可以修改图片信息
(发现错误后重新上传或者删除操作),审批提交的时候,前端提交一个人imgs信息列表,如果是新上传图片没有id信息,
如果是之前在库图片信息提交时会存在id信息.
</code></pre><h5 id="问题"><a href="#问题" class="headerlink" title="问题"></a>问题</h5><pre><code>列表提交到后台,一般的解决方案是将库中改退费id对应的图片信息删除,然后将前端提交的列表保存在数据库,
但是增加了数据库交互次数并且存在性能问题.
</code></pre><h5 id="解决方案"><a href="#解决方案" class="headerlink" title="解决方案"></a>解决方案</h5><pre><code>前端传来的图片列表信息在入库之前,和库中的数据对比分析得出哪些数据那要新增,哪些数据需要更新,
哪些数据需要删除,然后在执行持久化操作
</code></pre><h5 id="实现方式"><a href="#实现方式" class="headerlink" title="实现方式"></a>实现方式</h5><pre><code>在工程中需要添加一下包中的几个类:
</code></pre><img src="/typhoon/2017/09/17/基于业务的列表比较器/1.png" alt="result" title="result">  
<ol>
<li>IComparator:比较接口</li>
<li>AbstractComparator:对比抽象类,实现了一些通用操作,一些自定义操作使用末班方法交给子类去实现</li>
<li>CompareContext:对比上下文,也可以理解为一个容器,对比的数据都是从该类实例中获取</li>
<li>CompareRule:对比规则,使用者可以根据自身需要定义特定的比较规则</li>
<li>CompareResult:比较结果,比较完成后比较器会将结果(新增信息,更新信息,删除信息)放入此类实例返回</li>
<li><p>UserComparetor:这是一个自定义比较器,根据需要自己实现(该案例中我们比较用户信息)</p>
<p>下边贴出了各个类的代码实现:</p>
<p>IComparator:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div></pre></td><td class="code"><pre><div class="line">/**</div><div class="line">* 执行比较的接口</div><div class="line">* </div><div class="line">* @author Typhoon</div><div class="line">* @date 2017-09-17 10:20 Sunday</div><div class="line">* @since V2.0</div><div class="line">* @param &lt;S&gt;</div><div class="line">* @param &lt;T&gt;</div><div class="line">*/</div><div class="line">public abstract  interface  IComparator&lt;S,T&gt; &#123;</div><div class="line"></div><div class="line">  /**</div><div class="line">   * 执行比较操作</div><div class="line">   * </div><div class="line"> * @param paramCompareContext</div><div class="line"> * @return</div><div class="line"> */</div><div class="line">public abstract CompareResult&lt;T&gt; compare(CompareContext paramCompareContext);</div><div class="line"></div><div class="line">  /**</div><div class="line">   * 获取默认的对比规则</div><div class="line">   * </div><div class="line"> * @return</div><div class="line"> */</div><div class="line">public abstract CompareRule getDefaultRule();</div><div class="line"></div><div class="line">  /**</div><div class="line">   * 创建的时候执行一些初始化操作</div><div class="line">   * </div><div class="line"> * @param paramS</div><div class="line"> * @return</div><div class="line"> */</div><div class="line">public abstract T onCreate(S paramS);</div><div class="line"></div><div class="line">  /**</div><div class="line">   * 更新的时候执行初始化操作</div><div class="line">   * </div><div class="line"> * @param paramS</div><div class="line"> * @param paramT</div><div class="line"> */</div><div class="line">public abstract void onUpdate(S paramS, T paramT);</div><div class="line"></div><div class="line">  /**</div><div class="line">   * 删除的时候执行初始化操作</div><div class="line">   * </div><div class="line"> * @param paramT</div><div class="line"> */</div><div class="line">public abstract void onDelete(T paramT);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>AbstractComparator:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div><div class="line">120</div><div class="line">121</div><div class="line">122</div><div class="line">123</div><div class="line">124</div><div class="line">125</div><div class="line">126</div><div class="line">127</div><div class="line">128</div><div class="line">129</div><div class="line">130</div><div class="line">131</div><div class="line">132</div><div class="line">133</div><div class="line">134</div><div class="line">135</div><div class="line">136</div><div class="line">137</div><div class="line">138</div><div class="line">139</div><div class="line">140</div><div class="line">141</div><div class="line">142</div><div class="line">143</div><div class="line">144</div><div class="line">145</div><div class="line">146</div><div class="line">147</div><div class="line">148</div><div class="line">149</div><div class="line">150</div><div class="line">151</div><div class="line">152</div><div class="line">153</div><div class="line">154</div><div class="line">155</div><div class="line">156</div><div class="line">157</div><div class="line">158</div><div class="line">159</div><div class="line">160</div><div class="line">161</div><div class="line">162</div><div class="line">163</div><div class="line">164</div><div class="line">165</div><div class="line">166</div><div class="line">167</div><div class="line">168</div><div class="line">169</div><div class="line">170</div><div class="line">171</div><div class="line">172</div><div class="line">173</div><div class="line">174</div><div class="line">175</div><div class="line">176</div><div class="line">177</div><div class="line">178</div><div class="line">179</div><div class="line">180</div><div class="line">181</div><div class="line">182</div><div class="line">183</div><div class="line">184</div><div class="line">185</div><div class="line">186</div><div class="line">187</div><div class="line">188</div><div class="line">189</div><div class="line">190</div><div class="line">191</div><div class="line">192</div><div class="line">193</div><div class="line">194</div><div class="line">195</div><div class="line">196</div><div class="line">197</div><div class="line">198</div><div class="line">199</div><div class="line">200</div><div class="line">201</div><div class="line">202</div><div class="line">203</div><div class="line">204</div><div class="line">205</div><div class="line">206</div><div class="line">207</div><div class="line">208</div><div class="line">209</div><div class="line">210</div><div class="line">211</div><div class="line">212</div><div class="line">213</div><div class="line">214</div><div class="line">215</div><div class="line">216</div><div class="line">217</div><div class="line">218</div><div class="line">219</div><div class="line">220</div><div class="line">221</div><div class="line">222</div><div class="line">223</div><div class="line">224</div><div class="line">225</div><div class="line">226</div><div class="line">227</div><div class="line">228</div><div class="line">229</div><div class="line">230</div><div class="line">231</div><div class="line">232</div><div class="line">233</div><div class="line">234</div><div class="line">235</div><div class="line">236</div><div class="line">237</div><div class="line">238</div><div class="line">239</div><div class="line">240</div><div class="line">241</div><div class="line">242</div><div class="line">243</div><div class="line">244</div><div class="line">245</div><div class="line">246</div><div class="line">247</div><div class="line">248</div><div class="line">249</div><div class="line">250</div><div class="line">251</div><div class="line">252</div><div class="line">253</div><div class="line">254</div><div class="line">255</div><div class="line">256</div><div class="line">257</div><div class="line">258</div><div class="line">259</div><div class="line">260</div><div class="line">261</div><div class="line">262</div><div class="line">263</div><div class="line">264</div><div class="line">265</div><div class="line">266</div><div class="line">267</div><div class="line">268</div><div class="line">269</div><div class="line">270</div><div class="line">271</div><div class="line">272</div><div class="line">273</div><div class="line">274</div><div class="line">275</div><div class="line">276</div><div class="line">277</div><div class="line">278</div><div class="line">279</div></pre></td><td class="code"><pre><div class="line">/**</div><div class="line"> * 对比</div><div class="line"> * </div><div class="line"> * @author Typhoon</div><div class="line"> *</div><div class="line"> * @param &lt;S&gt;</div><div class="line"> * @param &lt;T&gt;</div><div class="line"> */</div><div class="line">public abstract class AbstractComparator&lt;S, T&gt; implements IComparator&lt;S, T&gt; &#123;</div><div class="line">private Logger logger = LoggerFactory.getLogger(AbstractComparator.class);</div><div class="line">private CompareRule defaultRule;</div><div class="line"></div><div class="line">public AbstractComparator() &#123;</div><div class="line"></div><div class="line">&#125;</div><div class="line"></div><div class="line">public CompareRule getDefaultRule() &#123;</div><div class="line">	return this.defaultRule;</div><div class="line">&#125;</div><div class="line"></div><div class="line">public void setDefaultRule(CompareRule defaultRule) &#123;</div><div class="line">	this.defaultRule = defaultRule;</div><div class="line">&#125;</div><div class="line"></div><div class="line">@SuppressWarnings(&#123; &quot;unchecked&quot;, &quot;rawtypes&quot; &#125;)</div><div class="line">public CompareResult&lt;T&gt; compare(CompareContext context) &#123;</div><div class="line">	CompareRule rule = (context.getRule() != null) ? context.getRule() : getDefaultRule();</div><div class="line"></div><div class="line">	Assert.notNull(rule, &quot;CompareRule can&apos;t be null.&quot;);</div><div class="line"></div><div class="line">	Object source = context.getSource();</div><div class="line">	Object target = context.getTarget();</div><div class="line"></div><div class="line">	Assert.notNull(source, &quot;Source can&apos;t be null.&quot;);</div><div class="line">	Assert.notNull(target, &quot;Target can&apos;t be null.&quot;);</div><div class="line"></div><div class="line">	if ((source instanceof Collection) &amp;&amp; (target instanceof Collection)) &#123;// 如果都是集合类型,直接比较</div><div class="line">		return doCompare((Collection) source, (Collection) target, rule);</div><div class="line">	&#125;</div><div class="line"></div><div class="line">	Collection sourceList;</div><div class="line">	if ((!(source instanceof Collection)) &amp;&amp; (!(target instanceof Collection))) &#123;// 如果都不是集合类型,转换成list比较</div><div class="line">		sourceList = new ArrayList(1);// 避免初始化过多内存空间</div><div class="line">		sourceList.add(source);</div><div class="line"></div><div class="line">		Collection targetList = new ArrayList();</div><div class="line">		targetList.add(target);</div><div class="line"></div><div class="line">		return doCompare(sourceList, targetList, rule);</div><div class="line">	&#125;</div><div class="line"></div><div class="line">	if ((!(source instanceof Collection)) &amp;&amp; (target instanceof Collection)) &#123;</div><div class="line">		sourceList = new ArrayList(1);</div><div class="line">		sourceList.add(source);</div><div class="line"></div><div class="line">		return doCompare(sourceList, (Collection) target, rule);</div><div class="line">	&#125;</div><div class="line"></div><div class="line">	if ((source instanceof Collection) &amp;&amp; (!(target instanceof Collection))) &#123;</div><div class="line">		Collection targetList = new ArrayList();</div><div class="line">		targetList.add(target);</div><div class="line"></div><div class="line">		return doCompare((Collection) source, targetList, rule);</div><div class="line">	&#125;</div><div class="line"></div><div class="line">	throw new IllegalArgumentException(String.format(&quot;Not support compare %s vs %s&quot;, new Object[] &#123; source, target &#125;));</div><div class="line">&#125;</div><div class="line"></div><div class="line">public void onUpdate(S source, T target) &#123;</div><div class="line">&#125;</div><div class="line"></div><div class="line">public void onDelete(T target) &#123;</div><div class="line">&#125;</div><div class="line"></div><div class="line">/**</div><div class="line"> * 真正的执行对比操作</div><div class="line"> * </div><div class="line"> * @author Typhoon</div><div class="line"> * @date 2017-09-17 10:25 Sunday</div><div class="line"> * @since V2.0</div><div class="line"> * @param source</div><div class="line"> * @param target</div><div class="line"> * @param rule</div><div class="line"> * @return</div><div class="line"> */</div><div class="line">@SuppressWarnings(&#123; &quot;rawtypes&quot;, &quot;unchecked&quot; &#125;)</div><div class="line">protected CompareResult&lt;T&gt; doCompare(Collection&lt;S&gt; source, Collection&lt;T&gt; target, CompareRule rule) &#123;</div><div class="line">	this.logger.info(&quot;comparing...&quot;);</div><div class="line">	long start = System.currentTimeMillis();</div><div class="line">	CompareResult&lt;T&gt; result = new CompareResult&lt;&gt;();</div><div class="line">	T tmpTarget;</div><div class="line">	Iterator i$;</div><div class="line">	if (null != source &amp;&amp; !source.isEmpty()) &#123;// 先遍历源数据,可以对比出需要增加和更新的内容</div><div class="line">		tmpTarget = null;</div><div class="line">		for (i$ = source.iterator(); i$.hasNext();) &#123;</div><div class="line">			S s = (S) i$.next();</div><div class="line">			if (isNeedCreate(s, target, rule)) &#123;// ①先检查是否有需要新增的</div><div class="line">				tmpTarget = onCreate(s);// 初始化一些属性</div><div class="line">				// 将源数据元素加入到新增列表</div><div class="line">				result.getNewList().add(tmpTarget);</div><div class="line">			&#125; else if ((tmpTarget = getUpdateObject(s, target, rule)) != null) &#123;// ②检查是否需要更新</div><div class="line">				// 有相等的元素</div><div class="line">				// 获取目标类的hashcode</div><div class="line">				int beforeUpdateHash = HashCodeBuilder.reflectionHashCode(tmpTarget, true);</div><div class="line"></div><div class="line">				if (rule.isAutoUpdate()) &#123;// 如果需要自动更新值,直接将源数据值复制到目标类中</div><div class="line">					copyProperties(s, tmpTarget);</div><div class="line">				&#125;</div><div class="line"></div><div class="line">				onUpdate(s, tmpTarget);// 触发更新的时候做额外一些业务,钩子方法</div><div class="line"></div><div class="line">				// 获取赋值后的目标数据hashcode,其实可以理解为源数据hashcode</div><div class="line">				int afterUpdateHash = HashCodeBuilder.reflectionHashCode(tmpTarget, true);</div><div class="line"></div><div class="line">				if (beforeUpdateHash != afterUpdateHash) &#123;// 如果不一致,就放入需要更新的列表中</div><div class="line">					result.getUpdateList().add(tmpTarget);</div><div class="line">				&#125;</div><div class="line">			&#125;</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line">	// Iterator i$;</div><div class="line">	if ((target != null) &amp;&amp; (target.size() &gt; 0)) &#123;</div><div class="line">		for (i$ = target.iterator(); i$.hasNext();) &#123;</div><div class="line">			T t = (T) i$.next();</div><div class="line">			// 将目标列表元素和源数据列表对比,如果源数据中没有,说明该元素需要删除</div><div class="line">			if (isNeedDelete(source, t, rule)) &#123;</div><div class="line">				onDelete(t);</div><div class="line">				result.getDeleteList().add(t);</div><div class="line">			&#125;</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line">	long end = System.currentTimeMillis();</div><div class="line">	this.logger.info(&quot;complete..time-consuming : &quot; + (end - start) + &quot;ms&quot;);</div><div class="line">	return result;</div><div class="line">&#125;</div><div class="line"></div><div class="line">/**</div><div class="line"> * 将源数据内容复制到目标数据中</div><div class="line"> * </div><div class="line"> * @param source</div><div class="line"> * @param target</div><div class="line"> */</div><div class="line">protected void copyProperties(S source, T target) &#123;</div><div class="line">	try &#123;</div><div class="line">		PropertyUtils.copyProperties(target, source);</div><div class="line">	&#125; catch (Exception e) &#123;</div><div class="line">		this.logger.error(&quot;Error occur:&quot;, e);</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">/**</div><div class="line"> * 根据对比规则和数据返回唯一结果</div><div class="line"> * </div><div class="line"> * @author Typhoon</div><div class="line"> * @date 2017-09-17 10:41 Sunday</div><div class="line"> * @since V2.0</div><div class="line"> * @param obj</div><div class="line"> * @param fields</div><div class="line"> * @param joinChar</div><div class="line"> * @param hash</div><div class="line"> * @return</div><div class="line"> */</div><div class="line">private String getCompareValue(Object obj, String[] fields, String joinChar, boolean hash) &#123;</div><div class="line">	Assert.notNull(obj, &quot;Object can&apos;t be null.&quot;);</div><div class="line">	Assert.notEmpty(fields, &quot;Compare fields can&apos;t be empty.&quot;);</div><div class="line"></div><div class="line">	StringBuffer sb = new StringBuffer();</div><div class="line">	try &#123;</div><div class="line">		// 用标记把value连起来</div><div class="line">		Object tmp = null;</div><div class="line">		for (String field : fields) &#123;// 将对比规则中需要比较的属性和对应的值使用连接符号拼接起来,类似id_1</div><div class="line">			if ((joinChar != null) &amp;&amp; (sb.length() &gt; 0)) &#123;</div><div class="line">				sb.append(joinChar);</div><div class="line">			&#125;</div><div class="line">			tmp = PropertyUtils.getProperty(obj, field);</div><div class="line">			sb.append((tmp == null) ? &quot;&quot; : tmp.toString());</div><div class="line">		&#125;</div><div class="line">	&#125; catch (Exception e) &#123;</div><div class="line">		this.logger.error(&quot;Error occur:&quot;, e);</div><div class="line">	&#125;</div><div class="line">	String value = sb.toString();</div><div class="line">	return ((hash) ? Md5.getMD5ofStr(value) : value);// 将拼接结果转换成字符串后返回(唯一字符串)</div><div class="line">&#125;</div><div class="line"></div><div class="line">/**</div><div class="line"> * 判断源数据和目标数据是否相等</div><div class="line"> * &lt;p&gt;</div><div class="line"> * 比较规则自定义</div><div class="line"> * &lt;/p&gt;</div><div class="line"> * </div><div class="line"> * @author Typhoon</div><div class="line"> * @date 2017-09-17 10:34 Sunday</div><div class="line"> * @since V2.0</div><div class="line"> * @param source</div><div class="line"> * @param target</div><div class="line"> * @param rule</div><div class="line"> * @return</div><div class="line"> */</div><div class="line">private boolean equals(Object source, Object target, CompareRule rule) &#123;</div><div class="line">	Assert.notNull(rule, &quot;CompareRule can&apos;t be null.&quot;);</div><div class="line">	// 根据属性比较两个对象是否相等</div><div class="line">	String sValue = getCompareValue(source, rule.getSourceAttrs(), rule.getJoinChar(), rule.isHash());</div><div class="line"></div><div class="line">	String tValue = getCompareValue(target, rule.getTargetAttrs(), rule.getJoinChar(), rule.isHash());</div><div class="line"></div><div class="line">	return sValue.equals(tValue);</div><div class="line">&#125;</div><div class="line"></div><div class="line">/**</div><div class="line"> * 由源数据单元素和目标列表对比,检查是否需要新增</div><div class="line"> * </div><div class="line"> * @author Typhoon</div><div class="line"> * @date 2017-09-17 10:29 Sunday</div><div class="line"> * @since V2.0</div><div class="line"> * @param s 源数据单个元素</div><div class="line"> * @param target 目标列表</div><div class="line"> * @param rule 比较规则</div><div class="line"> * @return</div><div class="line"> */</div><div class="line">private boolean isNeedCreate(S s, Collection&lt;T&gt; target, CompareRule rule) &#123;</div><div class="line">	Iterator&lt;T&gt; i$;</div><div class="line">	if (null == target || target.isEmpty()) &#123;// 目标没有数据,直接判定需要新增</div><div class="line">		return true;</div><div class="line">	&#125;</div><div class="line">	for (i$ = target.iterator(); i$.hasNext();) &#123;</div><div class="line">		Object t = i$.next();</div><div class="line">		if (equals(s, t, rule)) &#123;// 如果找到目标列表中与源数据匹配的数据,说明改数据存在,不需要新增</div><div class="line">			return false;</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line">	return true;</div><div class="line">&#125;</div><div class="line"></div><div class="line">/**</div><div class="line"> * 从源数据获取需要更新的元素</div><div class="line"> * </div><div class="line"> * @param s</div><div class="line"> * @param target</div><div class="line"> * @param rule</div><div class="line"> * @return</div><div class="line"> */</div><div class="line">private T getUpdateObject(S s, Collection&lt;T&gt; target, CompareRule rule) &#123;</div><div class="line">	Iterator&lt;T&gt; i$;</div><div class="line">	if (null == target || target.isEmpty()) &#123;</div><div class="line">		return null;</div><div class="line">	&#125;</div><div class="line">	for (i$ = target.iterator(); i$.hasNext();) &#123;</div><div class="line">		T t = i$.next();</div><div class="line">		if (equals(s, t, rule)) &#123;// 如果有判定属性相等的内容,返回目标列表中的该元素</div><div class="line">			return t;</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line">	return null;</div><div class="line">&#125;</div><div class="line"></div><div class="line">/**</div><div class="line"> * 检查是否需要删除</div><div class="line"> * </div><div class="line"> * @author Typhoon</div><div class="line"> * @date 2017-09-17 11:02 Sunday</div><div class="line"> * @since V2.0</div><div class="line"> * @param source</div><div class="line"> * @param t</div><div class="line"> * @param rule</div><div class="line"> * @return</div><div class="line"> */</div><div class="line">private boolean isNeedDelete(Collection&lt;S&gt; source, T t, CompareRule rule) &#123;</div><div class="line">	Iterator&lt;S&gt; i$;</div><div class="line">	if (null == source || source.isEmpty()) &#123;</div><div class="line">		return true;</div><div class="line">	&#125;</div><div class="line">	for (i$ = source.iterator(); i$.hasNext();) &#123;</div><div class="line">		Object s = i$.next();</div><div class="line">		if (equals(s, t, rule)) &#123;</div><div class="line">			return false;</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line">	return true;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>CompareContext:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div></pre></td><td class="code"><pre><div class="line">/**</div><div class="line"> * 对比上下文(容器)</div><div class="line"> * </div><div class="line"> * @author Typhoon</div><div class="line"> * @date 2017-09-17 10:18 Sunday</div><div class="line"> * @since V2.0</div><div class="line"> */</div><div class="line">public class CompareContext &#123;</div><div class="line">/**</div><div class="line"> * 源数据</div><div class="line"> */</div><div class="line">private Object source;</div><div class="line">/**</div><div class="line"> * 目标数据</div><div class="line"> */</div><div class="line">private Object target;</div><div class="line">/**</div><div class="line"> * 对比规则</div><div class="line"> */</div><div class="line">private CompareRule rule;</div><div class="line"></div><div class="line">public CompareContext() &#123;</div><div class="line">&#125;</div><div class="line"></div><div class="line">public CompareContext(Object source, Object target) &#123;</div><div class="line">	this(source, target, null);</div><div class="line">&#125;</div><div class="line"></div><div class="line">public CompareContext(Object source, Object target, CompareRule rule) &#123;</div><div class="line">	this.source = source;</div><div class="line">	this.target = target;</div><div class="line">	this.rule = rule;</div><div class="line">&#125;</div><div class="line"></div><div class="line">public Object getSource() &#123;</div><div class="line">	return this.source;</div><div class="line">&#125;</div><div class="line"></div><div class="line">public void setSource(Object source) &#123;</div><div class="line">	this.source = source;</div><div class="line">&#125;</div><div class="line"></div><div class="line">public Object getTarget() &#123;</div><div class="line">	return this.target;</div><div class="line">&#125;</div><div class="line"></div><div class="line">public void setTarget(Object target) &#123;</div><div class="line">	this.target = target;</div><div class="line">&#125;</div><div class="line"></div><div class="line">public CompareRule getRule() &#123;</div><div class="line">	return this.rule;</div><div class="line">&#125;</div><div class="line"></div><div class="line">public void setRule(CompareRule rule) &#123;</div><div class="line">	this.rule = rule;</div><div class="line">&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
</li>
</ol>
<p>CompareRule:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div></pre></td><td class="code"><pre><div class="line">/**</div><div class="line"> * 对比规则</div><div class="line"> * </div><div class="line"> * @author Typhoon</div><div class="line"> * @date 2017-09-17 11:07 Sunday</div><div class="line"> * @since V2.0</div><div class="line"> */</div><div class="line">public class CompareRule &#123;</div><div class="line">/**</div><div class="line"> * 源数据属性</div><div class="line"> */</div><div class="line">private String[] sourceAttrs;</div><div class="line"></div><div class="line">/**</div><div class="line"> * 目标属性</div><div class="line"> */</div><div class="line">private String[] targetAttrs;</div><div class="line"></div><div class="line">/**</div><div class="line"> * 连接字符</div><div class="line"> */</div><div class="line">private String joinChar;</div><div class="line"></div><div class="line">/**</div><div class="line"> * 是否需要hash计算</div><div class="line"> */</div><div class="line">private boolean hash;</div><div class="line"></div><div class="line">/**</div><div class="line"> * 是否自行更新</div><div class="line"> */</div><div class="line">private boolean autoUpdate;</div><div class="line"></div><div class="line">public CompareRule() &#123;</div><div class="line">	this.joinChar = &quot;_&quot;;</div><div class="line">&#125;</div><div class="line"></div><div class="line">public String[] getSourceAttrs() &#123;</div><div class="line">	return this.sourceAttrs;</div><div class="line">&#125;</div><div class="line"></div><div class="line">public void setSourceAttrs(String[] sourceAttrs) &#123;</div><div class="line">	this.sourceAttrs = ((String[]) ArrayUtils.clone(sourceAttrs));</div><div class="line">&#125;</div><div class="line"></div><div class="line">public String[] getTargetAttrs() &#123;</div><div class="line">	return this.targetAttrs;</div><div class="line">&#125;</div><div class="line"></div><div class="line">public void setTargetAttrs(String[] targetAttrs) &#123;</div><div class="line">	this.targetAttrs = ((String[]) ArrayUtils.clone(targetAttrs));</div><div class="line">&#125;</div><div class="line"></div><div class="line">public String getJoinChar() &#123;</div><div class="line">	return this.joinChar;</div><div class="line">&#125;</div><div class="line"></div><div class="line">public void setJoinChar(String joinChar) &#123;</div><div class="line">	this.joinChar = joinChar;</div><div class="line">&#125;</div><div class="line"></div><div class="line">public boolean isHash() &#123;</div><div class="line">	return this.hash;</div><div class="line">&#125;</div><div class="line"></div><div class="line">public void setHash(boolean hash) &#123;</div><div class="line">	this.hash = hash;</div><div class="line">&#125;</div><div class="line"></div><div class="line">public boolean isAutoUpdate() &#123;</div><div class="line">	return this.autoUpdate;</div><div class="line">&#125;</div><div class="line"></div><div class="line">public void setAutoUpdate(boolean autoUpdate) &#123;</div><div class="line">	this.autoUpdate = autoUpdate;</div><div class="line">&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>CompareResult:<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div></pre></td><td class="code"><pre><div class="line">/**</div><div class="line"> * 对比结果容器</div><div class="line"> * </div><div class="line"> * @author Typhoon</div><div class="line"> * @date 2017-09-17 11:04 Sunday</div><div class="line"> * @since V2.0</div><div class="line"> * @param &lt;T&gt;</div><div class="line"> */</div><div class="line">public class CompareResult&lt;T&gt; &#123;</div><div class="line">/**</div><div class="line"> * 需要更新的数据列表</div><div class="line"> */</div><div class="line">private List&lt;T&gt; updateList;</div><div class="line"></div><div class="line">/**</div><div class="line"> * 需要删除的数据列表</div><div class="line"> */</div><div class="line">private List&lt;T&gt; deleteList;</div><div class="line"></div><div class="line">/**</div><div class="line"> * 需要新增的数据列表</div><div class="line"> */</div><div class="line">private List&lt;T&gt; newList;</div><div class="line"></div><div class="line">public CompareResult() &#123;</div><div class="line">this.updateList = new ArrayList&lt;&gt;();</div><div class="line"></div><div class="line">this.deleteList = new ArrayList&lt;&gt;();</div><div class="line"></div><div class="line">this.newList = new ArrayList&lt;&gt;();</div><div class="line">&#125;</div><div class="line"></div><div class="line">/**</div><div class="line"> * 总共需要改变的数量</div><div class="line"> * </div><div class="line"> * @author Typhoon</div><div class="line"> * @date 2017-09-17 11:05 Sunday</div><div class="line"> * @since V2.0</div><div class="line"> * @return</div><div class="line"> */</div><div class="line">public int getChangeCount() &#123;</div><div class="line">	return (this.updateList.size() + this.deleteList.size() + this.newList.size());</div><div class="line">&#125;</div><div class="line"></div><div class="line">public List&lt;T&gt; getUpdateList() &#123;</div><div class="line">	return this.updateList;</div><div class="line">&#125;</div><div class="line"></div><div class="line">public void setUpdateList(List&lt;T&gt; updateList) &#123;</div><div class="line">	this.updateList = updateList;</div><div class="line">&#125;</div><div class="line"></div><div class="line">public List&lt;T&gt; getDeleteList() &#123;</div><div class="line">	return this.deleteList;</div><div class="line">&#125;</div><div class="line"></div><div class="line">public void setDeleteList(List&lt;T&gt; deleteList) &#123;</div><div class="line">	this.deleteList = deleteList;</div><div class="line">&#125;</div><div class="line"></div><div class="line">public List&lt;T&gt; getNewList() &#123;</div><div class="line">	return this.newList;</div><div class="line">&#125;</div><div class="line"></div><div class="line">public void setNewList(List&lt;T&gt; newList) &#123;</div><div class="line">	this.newList = newList;</div><div class="line">&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<pre><code>UserComparator:
</code></pre><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line">/**</div><div class="line"> * 用户信息比较器</div><div class="line"> * </div><div class="line"> * @author Typhoon</div><div class="line"> * @date 2017-09-17 11:12 Sunday</div><div class="line"> * @since V2.0</div><div class="line"> */</div><div class="line">public class UserComparetor extends AbstractComparator&lt;User, User&gt; &#123;</div><div class="line"></div><div class="line">@Override</div><div class="line">public User onCreate(User paramS) &#123;</div><div class="line">	paramS.setCreateTime(new Date());</div><div class="line">	return paramS;</div><div class="line">&#125;</div><div class="line"></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<pre><code>下边是具体业务实现:
</code></pre><img src="/typhoon/2017/09/17/基于业务的列表比较器/2.png" alt="result" title="result">
<pre><code>然后编写单元测试类模拟客户端提交:
</code></pre><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div></pre></td><td class="code"><pre><div class="line">/**</div><div class="line"> * 模拟用户信息对比客户端</div><div class="line"> * </div><div class="line"> * @author Typhoon</div><div class="line"> *</div><div class="line"> */</div><div class="line">public class UserCompareConsumer &#123;</div><div class="line"></div><div class="line">public static void main(String[] args) throws ParseException &#123;</div><div class="line">	</div><div class="line">	List&lt;User&gt; sourceList = new ArrayList&lt;&gt;(2);</div><div class="line">	User u = new User();</div><div class="line">	u.setName(&quot;aeolus&quot;);</div><div class="line">	sourceList.add(u);</div><div class="line">	u = new User();</div><div class="line">	u.setId(2L);</div><div class="line">	u.setName(&quot;typhoon&quot;);</div><div class="line">	SimpleDateFormat format = new SimpleDateFormat(&quot;yyyy-MM-dd HH:mm:ss&quot;);</div><div class="line">	u.setCreateTime(format.parse(&quot;2017-09-17 11:14:03&quot;));</div><div class="line">	sourceList.add(u);</div><div class="line">	new ClassPathXmlApplicationContext(&quot;spring-root.xml&quot;).start();</div><div class="line">	</div><div class="line">	UserService userService = SpringContextUtil.getBean(&quot;userService&quot;, UserService.class);</div><div class="line">	CompareResult&lt;User&gt;  result =  userService.compareUserList(sourceList);</div><div class="line">	System.out.println(JSON.toJSONString(result));</div><div class="line"></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<pre><code>运行程序得到如下结果:
</code></pre><img src="/typhoon/2017/09/17/基于业务的列表比较器/3.png" alt="result" title="result">  
<pre><code>将结果格式化:
</code></pre><img src="/typhoon/2017/09/17/基于业务的列表比较器/4.png" alt="result" title="result">  
<pre><code>查看数据库中的目标数据如下:
</code></pre><img src="/typhoon/2017/09/17/基于业务的列表比较器/5.png" alt="result" title="result">    
<pre><code>对比分析,我们已经计算出了需要新增,更新和删除的数据,接下来自己实现响应的数据持久化操作就可以了
</code></pre><h5 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h5><pre><code>这种方式是牺牲一定的java性能,来换取数据库操作的性能,从逻辑层面和性能层面都是划得来的。如果发现有瑕疵或者纰漏的地方,还请各位多多指正!
</code></pre>  
	</div>
		<footer class="article-footer clearfix">
<div class="article-catetags">

<div class="article-categories">
  <span></span>
  <a class="article-category-link" href="/typhoon/categories/性能优化/">性能优化</a>
</div>


  <div class="article-tags">
  
  <span></span> <a href="/typhoon/tags/基于业务的列表比较器/">基于业务的列表比较器</a>
  </div>

</div>



	<div class="article-share" id="share">
	
	  <div data-url="http://scorpioaeolus.oschina.io/2017/09/17/基于业务的列表比较器/" data-title="基于业务的列表比较器 | Typhoon&#39;s code space" data-tsina="null" class="share clearfix">
	  </div>
	
	</div>


</footer>

   	       
	</article>
	
<nav class="article-nav clearfix">
 
 <div class="prev" >
 <a href="/typhoon/2017/09/17/PropertyChangeListener概念和用法/" title="PropertyChangeListener概念和用法">
  <strong>上一篇：</strong><br/>
  <span>
  PropertyChangeListener概念和用法</span>
</a>
</div>


<div class="next">
<a href="/typhoon/2017/08/29/spring-redis缓存注解/"  title="spring+redis缓存注解">
 <strong>下一篇：</strong><br/> 
 <span>spring+redis缓存注解
</span>
</a>
</div>

</nav>

	


</div>  
      <div class="openaside"><a class="navbutton" href="#" title="Show Sidebar"></a></div>

  <div id="toc" class="toc-aside">
  <strong class="toc-title">Contents</strong>
 
 <ol class="toc"><li class="toc-item toc-level-5"><a class="toc-link" href="#背景"><span class="toc-number">1.</span> <span class="toc-text">背景</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#问题"><span class="toc-number">2.</span> <span class="toc-text">问题</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#解决方案"><span class="toc-number">3.</span> <span class="toc-text">解决方案</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#实现方式"><span class="toc-number">4.</span> <span class="toc-text">实现方式</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#总结"><span class="toc-number">5.</span> <span class="toc-text">总结</span></a></li></ol>
 
  </div>

<div id="asidepart">
<div class="closeaside"><a class="closebutton" href="#" title="Hide Sidebar"></a></div>
<aside class="clearfix">

  


  
<div class="categorieslist">
	<p class="asidetitle">Categories</p>
		<ul>
		
		  
			<li><a href="/typhoon/categories/dubbo/" title="dubbo">dubbo<sup>8</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/java性能优化/" title="java性能优化">java性能优化<sup>17</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/java源码分析/" title="java源码分析">java源码分析<sup>1</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/motan/" title="motan">motan<sup>2</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/spring/" title="spring">spring<sup>11</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/springboot/" title="springboot">springboot<sup>28</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/springmvc/" title="springmvc">springmvc<sup>1</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/容器/" title="容器">容器<sup>1</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/工具/" title="工具">工具<sup>7</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/性能优化/" title="性能优化">性能优化<sup>5</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/架构/" title="架构">架构<sup>8</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/案例分析/" title="案例分析">案例分析<sup>13</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/缓存/" title="缓存">缓存<sup>4</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/设计模式/" title="设计模式">设计模式<sup>3</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/面试题/" title="面试题">面试题<sup>1</sup></a></li>
		  
		
		</ul>
</div>


  
<div class="tagslist">
	<p class="asidetitle">Tags</p>
		<ul class="clearfix">
		
			
				<li><a href="/typhoon/tags/springboot/" title="springboot">springboot<sup>28</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/java性能优化/" title="java性能优化">java性能优化<sup>9</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/限流/" title="限流">限流<sup>3</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/Spring5/" title="Spring5">Spring5<sup>3</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/设计模式/" title="设计模式">设计模式<sup>3</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/案例分析/" title="案例分析">案例分析<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/性能优化/" title="性能优化">性能优化<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/Spring/" title="Spring">Spring<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/dubbo/" title="dubbo">dubbo<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/分页/" title="分页">分页<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/并发/" title="并发">并发<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/异常拦截/" title="异常拦截">异常拦截<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/dubbo分布式事务/" title="dubbo分布式事务">dubbo分布式事务<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/自带图片/" title="自带图片">自带图片<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/svn导出mvn项目/" title="svn导出mvn项目">svn导出mvn项目<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/dubbo过滤器/" title="dubbo过滤器">dubbo过滤器<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/排序性能对比/" title="排序性能对比">排序性能对比<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/ListenableFuture/" title="ListenableFuture">ListenableFuture<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/PropertyChangeListener/" title="PropertyChangeListener">PropertyChangeListener<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/jdbcTemplate高可用/" title="jdbcTemplate高可用">jdbcTemplate高可用<sup>1</sup></a></li>
			
		
		</ul>
</div>


  <div class="linkslist">
  <p class="asidetitle">Links</p>
    <ul>
        
          <li>
            
            	<a href="https://coderq.com" target="_blank" title="一个面向程序员交流分享的新一代社区">码农圈</a>
            
          </li>
        
          <li>
            
            	<a href="http://wuchong.me" target="_blank" title="Jark&#39;s Blog">Jark&#39;s Blog</a>
            
          </li>
        
    </ul>
</div>

  


  <div class="rsspart">
	<a href="/atom.xml" target="_blank" title="rss">RSS</a>
</div>

  <div class="weiboshow">
  <p class="asidetitle">Weibo</p>
    <iframe width="100%" height="119" class="share_self"  frameborder="0" scrolling="no" src="http://widget.weibo.com/weiboshow/index.php?language=&width=0&height=119&fansRow=2&ptype=1&speed=0&skin=9&isTitle=1&noborder=1&isWeibo=0&isFans=0&uid=null&verifier=b3593ceb&dpc=1"></iframe>
</div>


</aside>
</div>
    </div>
    <footer><div id="footer" >
	
	<div class="line">
		<span></span>
		<div class="author"></div>
	</div>
	
	
	<section class="info">
		<p> Hello ,I&#39;m a java coder. <br/>
			This is my blog,believe it or not.</p>
	</section>
	 
	<div class="social-font" class="clearfix">
		
		
		
		
		
		
		
		
		
		
		<a href="mailto:ScorpioAeolus@163.com" target="_blank" class="icon-email" title="Email Me"></a>
		
	</div>
			
		

		<p class="copyright">
		Powered by <a href="http://hexo.io" target="_blank" title="hexo">hexo</a> and Theme by <a href="https://github.com/wuchong/jacman" target="_blank" title="Jacman">Jacman</a> © 2019 
		
		<a href="/typhoon/about" target="_blank" title="typhoon">typhoon</a>
		
		
		</p>
</div>
</footer>
    <script src="/typhoon/js/jquery-2.0.3.min.js"></script>
<script src="/typhoon/js/jquery.imagesloaded.min.js"></script>
<script src="/typhoon/js/gallery.js"></script>
<script src="/typhoon/js/jquery.qrcode-0.12.0.min.js"></script>

<script type="text/javascript">
$(document).ready(function(){ 
  $('.navbar').click(function(){
    $('header nav').toggleClass('shownav');
  });
  var myWidth = 0;
  function getSize(){
    if( typeof( window.innerWidth ) == 'number' ) {
      myWidth = window.innerWidth;
    } else if( document.documentElement && document.documentElement.clientWidth) {
      myWidth = document.documentElement.clientWidth;
    };
  };
  var m = $('#main'),
      a = $('#asidepart'),
      c = $('.closeaside'),
      o = $('.openaside');
  c.click(function(){
    a.addClass('fadeOut').css('display', 'none');
    o.css('display', 'block').addClass('fadeIn');
    m.addClass('moveMain');
  });
  o.click(function(){
    o.css('display', 'none').removeClass('beforeFadeIn');
    a.css('display', 'block').removeClass('fadeOut').addClass('fadeIn');      
    m.removeClass('moveMain');
  });
  $(window).scroll(function(){
    o.css("top",Math.max(80,260-$(this).scrollTop()));
  });
  
  $(window).resize(function(){
    getSize(); 
    if (myWidth >= 1024) {
      $('header nav').removeClass('shownav');
    }else{
      m.removeClass('moveMain');
      a.css('display', 'block').removeClass('fadeOut');
      o.css('display', 'none');
      
      $('#toc.toc-aside').css('display', 'none');
        
    }
  });
});
</script>

<script type="text/javascript">
$(document).ready(function(){ 
  var ai = $('.article-content>iframe'),
      ae = $('.article-content>embed'),
      t  = $('#toc'),
      ta = $('#toc.toc-aside'),
      o  = $('.openaside'),
      c  = $('.closeaside');
  if(ai.length>0){
    ai.wrap('<div class="video-container" />');
  };
  if(ae.length>0){
   ae.wrap('<div class="video-container" />');
  };
  c.click(function(){
    ta.css('display', 'block').addClass('fadeIn');
  });
  o.click(function(){
    ta.css('display', 'none');
  });
  $(window).scroll(function(){
    ta.css("top",Math.max(140,320-$(this).scrollTop()));
  });
});
</script>


<script type="text/javascript">
$(document).ready(function(){ 
  var $this = $('.share'),
      url = $this.attr('data-url'),
      encodedUrl = encodeURIComponent(url),
      title = $this.attr('data-title'),
      tsina = $this.attr('data-tsina'),
      description = $this.attr('description');
  var html = [
  '<div class="hoverqrcode clearfix"></div>',
  '<a class="overlay" id="qrcode"></a>',
  '<a href="https://www.facebook.com/sharer.php?u=' + encodedUrl + '" class="article-share-facebook" target="_blank" title="Facebook"></a>',
  '<a href="https://twitter.com/intent/tweet?url=' + encodedUrl + '" class="article-share-twitter" target="_blank" title="Twitter"></a>',
  '<a href="#qrcode" class="article-share-qrcode" title="微信"></a>',
  '<a href="http://widget.renren.com/dialog/share?resourceUrl=' + encodedUrl + '&srcUrl=' + encodedUrl + '&title=' + title +'" class="article-share-renren" target="_blank" title="人人"></a>',
  '<a href="http://service.weibo.com/share/share.php?title='+title+'&url='+encodedUrl +'&ralateUid='+ tsina +'&searchPic=true&style=number' +'" class="article-share-weibo" target="_blank" title="微博"></a>',
  '<span title="Share to"></span>'
  ].join('');
  $this.append(html);

  $('.hoverqrcode').hide();

  var myWidth = 0;
  function updatehoverqrcode(){
    if( typeof( window.innerWidth ) == 'number' ) {
      myWidth = window.innerWidth;
    } else if( document.documentElement && document.documentElement.clientWidth) {
      myWidth = document.documentElement.clientWidth;
    };
    var qrsize = myWidth > 1024 ? 200:100;
    var options = {render: 'image', size: qrsize, fill: '#2ca6cb', text: url, radius: 0.5, quiet: 1};
    var p = $('.article-share-qrcode').position();
    $('.hoverqrcode').empty().css('width', qrsize).css('height', qrsize)
                          .css('left', p.left-qrsize/2+20).css('top', p.top-qrsize-10)
                          .qrcode(options);
  };
  $(window).resize(function(){
    $('.hoverqrcode').hide();
  });
  $('.article-share-qrcode').click(function(){
    updatehoverqrcode();
    $('.hoverqrcode').toggle();
  });
  $('.article-share-qrcode').hover(function(){}, function(){
      $('.hoverqrcode').hide();
  });
});   
</script>









<link rel="stylesheet" href="/typhoon/fancybox/jquery.fancybox.css" media="screen" type="text/css">
<script src="/typhoon/fancybox/jquery.fancybox.pack.js"></script>
<script type="text/javascript">
$(document).ready(function(){ 
  $('.article-content').each(function(i){
    $(this).find('img').each(function(){
      if ($(this).parent().hasClass('fancybox')) return;
      var alt = this.alt;
      if (alt) $(this).after('<span class="caption">' + alt + '</span>');
      $(this).wrap('<a href="' + this.src + '" title="' + alt + '" class="fancybox"></a>');
    });
    $(this).find('.fancybox').each(function(){
      $(this).attr('rel', 'article' + i);
    });
  });
  if($.fancybox){
    $('.fancybox').fancybox();
  }
}); 
</script>



<!-- Analytics Begin -->



<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "//hm.baidu.com/hm.js?e6d1f421bbc9962127a50488f9ed37d1";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>



<!-- Analytics End -->

<!-- Totop Begin -->

	<div id="totop">
	<a title="Back to Top"><img src="/typhoon/img/scrollup.png"/></a>
	</div>
	<script src="/typhoon/js/totop.js"></script>

<!-- Totop End -->

<!-- MathJax Begin -->
<!-- mathjax config similar to math.stackexchange -->


<!-- MathJax End -->

<!-- Tiny_search Begin -->

<!-- Tiny_search End -->

  </body>
</html>
